package com.zixuan.gulicollege.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zixuan.gulicollege.common_utils.ResultCode;
import com.zixuan.gulicollege.eduservice.service.EduSubjectService;
import com.zixuan.gulicollege.entity.eduservice.EduSubject;
import com.zixuan.gulicollege.entity.eduservice.excel.SubjectData;
import com.zixuan.gulicollege.exception_handler.ConstantExceptionInfo;
import com.zixuan.gulicollege.exception_handler.GuliException;

/**
 * @ClassName SubjectExcelListener
 * @Author by zixuan
 * @Date 2021/5/6 19:33
 */

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    private EduSubjectService subjectService;

    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    public SubjectExcelListener() {}

    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        String parentId = "0";

        if (subjectData == null){
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.NO_CONTENT_FILE);
        }

        EduSubject eduOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());

        // 判断当前一级分类是否存在如果不存在那么就进行添加
        if (eduOneSubject == null) {
            eduOneSubject = new EduSubject();
            eduOneSubject.setParentId(parentId);
            // 设置一级分类名称
            eduOneSubject.setTitle(subjectData.getOneSubjectName());
            subjectService.save(eduOneSubject);
        }

        // 获取当前一级分类的id
        String pid = eduOneSubject.getId();

        EduSubject eduTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);

        // 判断当前二级分类是否存在如果不存在那么就进行添加
        if (eduTwoSubject == null) {
            eduTwoSubject = new EduSubject();
            eduTwoSubject.setParentId(pid);
            // 设置二级分类名称
            eduTwoSubject.setTitle(subjectData.getTwoSubjectName());
            subjectService.save(eduTwoSubject);
        }
    }

    /**
     * 判断一级分类是否重复添加
     * @param subjectService
     * @param name
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService, String name){
        // 获取一级分类
        String parentId = "0";
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();

        wrapper.eq("title", name);

        wrapper.eq("parent_id", parentId);

        EduSubject oneSubject = subjectService.getOne(wrapper);

        return oneSubject;
    }

    /**
     * 获取二级分类是否重复添加
     * @param subjectService
     * @param name
     * @param pid
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService, String name, String pid){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();

        wrapper.eq("title", name);

        wrapper.eq("parent_id", pid);

        EduSubject TwoSubject = subjectService.getOne(wrapper);

        return TwoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
